package com.zengdb.crm.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zengdb.crm.domain.User;
import com.zengdb.crm.page.AjaxResult;
import com.zengdb.crm.page.PageResult;
import com.zengdb.crm.query.UserQueryObject;
import com.zengdb.crm.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	IUserService userService;
	
	@RequestMapping("")
	public String index(){
		return "user";
	}
	@RequestMapping("/list")
	@ResponseBody
	public PageResult list(UserQueryObject qo){
		PageResult pageResult = null;
		pageResult = userService.queryPage(qo);
		return pageResult;
	}
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResult save(User user){
		AjaxResult result = null;
		try{
			userService.insert(user);
			result = new AjaxResult(true,"保存成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("保存失败,请联系管理员！");
		}
		return result;
	}
	@RequestMapping("/update")
	@ResponseBody
	public AjaxResult update(User user){
		AjaxResult result = null;
		try{
			userService.updateByPrimaryKey(user);
			result = new AjaxResult(true,"更新成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("更新失败,请联系管理员！");
		}
		return result;
	}
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResult delete(Long userId){
		AjaxResult result = null;
		try{
			userService.deleteByPrimaryKey(userId);
			result = new AjaxResult(true,"删除成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("删除失败,请联系管理员！");
		}
		return result;
	}
}
